package com.mei.demo.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mei.demo.dao.DictionaryDao;
import com.mei.demo.domain.Dictionary;
import com.mei.demo.domain.DictionarySearcher;
import com.mei.demo.enums.DictionaryTypeEnum;
import com.mei.demo.service.DictionaryGroupService;

/**
 * 数据字典组
 * 
 * 2018年7月11日 下午8:57:58 mei.xiaoliang@qq.com
 */
@Service
public class DictionaryGroupServiceImpl implements DictionaryGroupService {

    @Value("${server.port}")
    private String        port;

    @Autowired
    private DictionaryDao dictionaryMapper;

    /**
     * @see com.mei.demo.service.DictionaryGroupService#searchPage()
     */
    @Override
    public IPage<Dictionary> searchPage(DictionarySearcher searcher) {

        QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<Dictionary>();
        IPage<Dictionary> result = dictionaryMapper.selectPage(new Page<Dictionary>(searcher.getPageNum(), searcher.getPageSize()), queryWrapper);

        System.out.println(result);

        return result;
    }

    /**
     * @see com.mei.demo.service.DictionaryGroupService#browse(int)
     */
    @Override
    public Dictionary browse(int id) {
        Dictionary dictionary = dictionaryMapper.browse(id);
        return dictionary;
    }

    @Override
    public boolean queryGroupByCode(Integer id, Integer code) {

        boolean result = false;

        Dictionary query = new Dictionary();
        query.setCode(code);
        query.setType(DictionaryTypeEnum.GROUP.getCode());

        Dictionary dictionary = dictionaryMapper.queryGroupByCode(query);

        if (dictionary == null || (dictionary != null && id.intValue() == dictionary.getId().intValue())) {
            result = true;
        }

        return result;
    }

    /** 
     * @see com.mei.demo.service.DictionaryGroupService#add(com.mei.demo.domain.Dictionary)
     */
    @Override
    public int add(Dictionary dictionary) {
        return dictionaryMapper.insert(dictionary);
    }

    /** 
     * @see com.mei.demo.service.DictionaryGroupService#update(com.mei.demo.domain.Dictionary)
     */
    @Override
    public int update(Dictionary dictionary) {
        int result = dictionaryMapper.update(dictionary);
        return result;
    }

    /** 
     * @see com.mei.demo.service.DictionaryGroupService#delete(com.mei.demo.domain.Dictionary)
     */
    @Override
    public int delete(Dictionary dictionary) {
        return dictionaryMapper.delete(dictionary);
    }

}
